package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.WeekTimeBean;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.Constant;
import com.example.peibei.ui.adapter.ChooseWeekTimeAdapter;
import com.linxiao.framework.common.ScreenUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.listeners.OnMoreItemPickListener;
import cn.addapp.pickers.listeners.OnMoreWheelListener;
import cn.addapp.pickers.picker.LinkagePicker;

public class ChooseOrderTimeActivity extends WDActivity {
    @BindView(R.id.rv_week_time)
    RecyclerView rv_week_time;

    List<WeekTimeBean> weekTimes = new ArrayList<>();
    private ChooseWeekTimeAdapter chooseWeekTimeAdapter;
    private String startTime = "00:00";
    private String endTime = "23:00";
    private int categoryId;
    private String timeLimit = "1_00:00-23:00";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_order_time;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        categoryId = bundle.getInt("categoryId");
        rv_week_time.setLayoutManager(new LinearLayoutManager(this));
        chooseWeekTimeAdapter = new ChooseWeekTimeAdapter(weekTimes);
        rv_week_time.setAdapter(chooseWeekTimeAdapter);
        getWeeks();
        weekTimes.get(0).setChoosed(true);
        chooseWeekTimeAdapter.notifyDataSetChanged();
        chooseWeekTimeAdapter.addChildClickViewIds(R.id.ll_week_time);
        chooseWeekTimeAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.ll_week_time) {
                    showChooseTime(position);
                }
            }
        });
        chooseWeekTimeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                weekTimes.get(position).setChoosed(!weekTimes.get(position).isChoosed());
                chooseWeekTimeAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.tv_next)
    public void next() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < weekTimes.size(); i++) {
            if (weekTimes.get(i).isChoosed()) {
                if (i == weekTimes.size() - 1) {
                    str.append(weekTimes.get(i).getWeek() + "_" + weekTimes.get(i).getStartTime() + "-" + weekTimes.get(i).getEndTime());
                } else {
                    str.append(weekTimes.get(i).getWeek() + "_" + weekTimes.get(i).getStartTime() + "-" + weekTimes.get(i).getEndTime());
                    str.append(",");
                }
            }
        }
        timeLimit = str.toString();
        if (TextUtils.isEmpty(timeLimit)){
            UIUtils.showToastSafe("请至少选择一个时间段");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putInt("categoryId",categoryId);
        bundle.putString("timeLimit",timeLimit);
        intent(ChooseOrderContentActivity.class,bundle);
//        finish();
    }

    private void getWeeks() {
        weekTimes.add(new WeekTimeBean(1, "00:00", "23:00", "周一"));
        weekTimes.add(new WeekTimeBean(2, "00:00", "23:00", "周二"));
        weekTimes.add(new WeekTimeBean(3, "00:00", "23:00", "周三"));
        weekTimes.add(new WeekTimeBean(4, "00:00", "23:00", "周四"));
        weekTimes.add(new WeekTimeBean(5, "00:00", "23:00", "周五"));
        weekTimes.add(new WeekTimeBean(6, "00:00", "23:00", "周六"));
        weekTimes.add(new WeekTimeBean(7, "00:00", "23:00", "周日"));
    }

    private void showChooseTime(int position) {
        LinkagePicker.DataProvider provider = new LinkagePicker.DataProvider() {

            @Override
            public boolean isOnlyTwo() {
                return true;
            }

            @Override
            public List<String> provideFirstData() {
                return Arrays.asList(Constant.times);
            }

            @Override
            public List<String> provideSecondData(int firstIndex) {
                return Arrays.asList(Constant.times);
            }

            @Override
            public List<String> provideThirdData(int firstIndex, int secondIndex) {
                return null;
            }

        };
        LinkagePicker picker = new LinkagePicker(this, provider);
        Window window = picker.getWindow();
        if (window != null) {
//            window.setGravity(Gravity.BOTTOM);
            ////解决宽度问题
            window.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
            window.setBackgroundDrawableResource(R.drawable.shape_white_bg);
        }
        picker.setTextSize(18);
        picker.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        picker.setHeight(ScreenUtil.getRealScreenWidth());
        picker.setCanLoop(false);
        picker.setSelectedIndex(0, 23);
        picker.setGravity(Gravity.BOTTOM);
        picker.setLineColor(ContextCompat.getColor(this, R.color.color_EEEEEE));
        picker.setSelectedTextColor(ContextCompat.getColor(this, R.color.text_dark_gray));//前四位值是透明度
        picker.setUnSelectedTextColor(ContextCompat.getColor(this, R.color.color_9C9DA1));
//        picker.getWindow().setBackgroundDrawableResource(R.drawable.shape_white_bg);
        picker.setOnMoreWheelListener(new OnMoreWheelListener() {
            @Override
            public void onFirstWheeled(int index, String item) {
                startTime = item;
            }

            @Override
            public void onSecondWheeled(int index, String item) {
                endTime = item;
            }

            @Override
            public void onThirdWheeled(int index, String item) {

            }
        });
        picker.setOnMoreItemPickListener((OnMoreItemPickListener<String>) (first, second, third) -> {
            startTime = first;
            endTime = second;
            String[] split = startTime.split(":");
            String[] split1 = endTime.split(":");
            String a = split[0];
            String b = split1[0];
            int i = Integer.parseInt(a);
            int i1 = Integer.parseInt(b);
            if (i > i1) {
                UIUtils.showToastSafe("开始时间不能超过结束时间，请重新选择");
            } else {
                weekTimes.get(position).setStartTime(startTime);
                weekTimes.get(position).setEndTime(endTime);
                chooseWeekTimeAdapter.notifyDataSetChanged();
            }
        });
        picker.show();
    }

    @Override
    protected void destoryData() {

    }
}
